﻿using CanalService.EFCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NCD.ORMDbContext.Dapper;
using NCD.ORMDbContext.EFCore;
using NCD.ORMDbContext.Entities;
using NCD.ORMDbContext.Enums;
using NCD.ORMDbContext.SqlSugarOrm; // 引入Host扩展


// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

/**
 * Dapper.SqlBuilder:Dapper基础操作封装，
 * 
 * **/
var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContext, services) =>
    {
        // 注册应用程序的服务。这可以包括框架服务和用户定义的服务。
        // 例如，注册Dapper DbContext
        //services.AddDapperDBContext<ProjectDapperContext>(options =>
        //{
        //    // 配置Dapper DbContext所需的选项
        //    //var connectionString = hostContext.Configuration.GetConnectionString("DefaultConnection");
        //    var connectionString = "Server=localhost;Port=3306;Database=ndc_test;Uid=root;Pwd=123456;";
        //    options.Connection = connectionString;
        //    options.DbType = DbTypeNameEnum.MySQL;
        //});

        //services.AddSqlSugarDBContext<ProjectSqlSugarContext>(options =>
        //{
        //    // 配置Dapper DbContext所需的选项
        //    //var connectionString = hostContext.Configuration.GetConnectionString("DefaultConnection");
        //    var connectionString = "Server=localhost;Port=3306;Database=ndc_test;Uid=root;Pwd=123456;";
        //    options.Connection = connectionString;
        //    options.DbType = DbTypeNameEnum.MySQL;
        //});


        services.AddEFCoreDBContext<ProjectEFCoreContext>(options =>
        {
            var connectionString = "Server=localhost;Port=3306;Database=ndc_test;Uid=root;Pwd=123456;";
            options.Connection = connectionString;
            options.DbType = DbTypeNameEnum.MySQL;
        });

    })
    .Build();

//Dapper
//var application = host.Services.GetService<ProjectDapperContext>();
//var list = application.GetAllAsync<UserInfo>().Result.ToList();


//SqlSugar
//var application = host.Services.GetService<ProjectSqlSugarContext>();
//var list = application.DbContext.Queryable<SugarUserInfo>().ToList();

//EFCore

var application = host.Services.GetService<ProjectEFCoreContext>();
var list = application.UserInfo.ToList();

Console.WriteLine($"-------执行结果---{application.Connection.ConnectionString}-------");
Console.WriteLine(list.FirstOrDefault()?.Name);
 
// 运行应用程序
await host.RunAsync();